from typing import Optional, List


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def dfs(root: Optional[TreeNode], ans: List[int]):
    if root is None:
        return
    ans.append(root.val)
    dfs(root.left, ans)
    dfs(root.right, ans)

class Solution:
    def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
        ans = []
        dfs(root, ans)
        return ans
